import { AdManager } from "../ads/AdManager";
import { Tools } from "../common/Tools";
import { AudioManager1 } from "../manager/AudioManager1";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SettingUI extends cc.Component {

    @property(cc.Node)
    musicBtn: cc.Node = null;
    @property(cc.Node)
    effectBtn: cc.Node = null;
    @property(cc.Node)
    vibBtn: cc.Node = null;
    @property(cc.Node)
    subtitleBtn: cc.Node = null;
    @property(cc.Node)
    dialogueBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "关闭按钮" })
    closeBtn: cc.Node = null;


    @property(cc.Node)
    WinReGameBtn: cc.Node = null;

    @property(cc.Node)
    TexiaoBtn: cc.Node = null;
    @property(cc.Node)
    liveLevelBtn: cc.Node = null;
    @property(cc.Node)
    autoAnswerBtn: cc.Node = null;
    @property(cc.Node)
    showLvNameBtn: cc.Node = null;
    @property(cc.Node)
    kbTipBtn: cc.Node = null;
    @property(cc.Node)
    answerRightBtn: cc.Node = null;

    static isWinReGameBtn: boolean = false;

    static isCliCkTexiaoBtn: boolean = false;

    static isLiveLevelBtn: boolean = false;

    static isAutoAnswerBtn: boolean = false;

    static isShowLvNameBtn: boolean = false;

    static isKbTipBtn: boolean = false;

    static isAnswerRightBtn: boolean = false;


    start() {
        this.closeBtn.on("click", () => {
            AudioManager1.playClick();
            UIManager.ins.close();
        });

        this.updateMusicBtn();
        this.updateEffectBtn();
        this.updateVibBtn();
        this.updateSubtitleBtn();
        this.updateDialogueBtn();


        let isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        console.log("isWinReGameBtnBendi", isWinReGameBtnBendi);

        if (isWinReGameBtnBendi) {
            this.WinReGameBtn.getComponent(cc.Toggle).check()
            SettingUI.isWinReGameBtn = true;
        }


        AdManager.isWinReGameBtn((active: boolean) => {
            this.WinReGameBtn.active = active;
        });

        let isCliCkTexiaoBtnBendi = Tools.getStorage('isCliCkTexiaoBtn')
        console.log("isCliCkTexiaoBtnBendi", isCliCkTexiaoBtnBendi);

        if (isCliCkTexiaoBtnBendi != "false") {
            this.TexiaoBtn.getComponent(cc.Toggle).check()
            SettingUI.isCliCkTexiaoBtn = true;
        }

        AdManager.isLiveLevelBtn((active: boolean) => {
            this.liveLevelBtn.active = active;
            this.autoAnswerBtn.active = false;
            this.showLvNameBtn.active = active;
            this.kbTipBtn.active = false;
            this.answerRightBtn.active = false;
        });


        let isLiveLevelActive = Tools.getStorage('setLiveLvActive')
        if (isLiveLevelActive != "false") {
            this.liveLevelBtn.getComponent(cc.Toggle).check();
            SettingUI.isLiveLevelBtn = true;
        } else {
            this.liveLevelBtn.getComponent(cc.Toggle).uncheck();
            SettingUI.isLiveLevelBtn = false;
        }

        let isAutoAnswerActive = Tools.getStorage('setAutoAnswerActive')
        if (isAutoAnswerActive && isAutoAnswerActive != "false") {
            this.autoAnswerBtn.getComponent(cc.Toggle).check();
            SettingUI.isAutoAnswerBtn = true;
        } else {
            this.autoAnswerBtn.getComponent(cc.Toggle).uncheck();
            SettingUI.isAutoAnswerBtn = false;
        }

        let isShowLvNameActive = Tools.getStorage('setShowLvNameActive')
        if (isShowLvNameActive && isShowLvNameActive != "false") {
            this.showLvNameBtn.getComponent(cc.Toggle).check();
            SettingUI.isShowLvNameBtn = true;
        } else {
            this.showLvNameBtn.getComponent(cc.Toggle).uncheck();
            SettingUI.isShowLvNameBtn = false;
        }

        let isKbTipActive = Tools.getStorage('setKbTipActive')
        if (isKbTipActive && isKbTipActive != "false") {
            this.kbTipBtn.getComponent(cc.Toggle).check();
            SettingUI.isKbTipBtn = true;
        } else {
            this.kbTipBtn.getComponent(cc.Toggle).uncheck();
            SettingUI.isKbTipBtn = false;
        }

        let isAnswerRightActive = Tools.getStorage('setAnswerRightActive')
        if (isAnswerRightActive && isAnswerRightActive != "false") {
            this.answerRightBtn.getComponent(cc.Toggle).check();
            SettingUI.isAnswerRightBtn = true;
        } else {
            this.answerRightBtn.getComponent(cc.Toggle).uncheck();
            SettingUI.isAnswerRightBtn = false;
        }

    }
    updateMusicBtn() {
        this.musicBtn.getChildByName('open').active = AudioManager1.isPlayBG;
        this.musicBtn.getChildByName('close').active = !AudioManager1.isPlayBG;
    }
    updateEffectBtn() {
        this.effectBtn.getChildByName('open').active = AudioManager1.isPlayAudio;
        this.effectBtn.getChildByName('close').active = !AudioManager1.isPlayAudio;
    }
    updateVibBtn() {
        this.vibBtn.getChildByName('open').active = AudioManager1.isVib;
        this.vibBtn.getChildByName('close').active = !AudioManager1.isVib;
    }
    updateSubtitleBtn() {
        this.subtitleBtn.getChildByName('open').active = AudioManager1.isSubtitle;
        this.subtitleBtn.getChildByName('close').active = !AudioManager1.isSubtitle;
    }
    updateDialogueBtn() {
        this.dialogueBtn.getChildByName('open').active = AudioManager1.isDialogue;
        this.dialogueBtn.getChildByName('close').active = !AudioManager1.isDialogue;
    }

    clickWinReGameBtn() {
        SettingUI.isWinReGameBtn = !SettingUI.isWinReGameBtn
        console.log("isWinReGameBtn", SettingUI.isWinReGameBtn);
        if (SettingUI.isWinReGameBtn) {
            Tools.setStorage('isWinReGameBtn', SettingUI.isWinReGameBtn)
        } else {
            Tools.setStorage('isWinReGameBtn', null)
        }
    }

    clickTexiaoBtn() {
        SettingUI.isCliCkTexiaoBtn = !SettingUI.isCliCkTexiaoBtn
        console.log("isCliCkTexiaoBtn", SettingUI.isCliCkTexiaoBtn);
        if (SettingUI.isCliCkTexiaoBtn) {
            Tools.setStorage('isCliCkTexiaoBtn', SettingUI.isCliCkTexiaoBtn)
        } else {
            Tools.setStorage('isCliCkTexiaoBtn', "false")
        }
    }

    clickLiveLvBtn() {
        SettingUI.isLiveLevelBtn = !SettingUI.isLiveLevelBtn;
        Tools.setStorage('setLiveLvActive', SettingUI.isLiveLevelBtn);
    }

    clickAutoAnswerBtn() {
        SettingUI.isAutoAnswerBtn = !SettingUI.isAutoAnswerBtn;
        Tools.setStorage('setAutoAnswerActive', SettingUI.isAutoAnswerBtn);
    }

    clickShowLvNameBtn() {
        SettingUI.isShowLvNameBtn = !SettingUI.isShowLvNameBtn;
        Tools.setStorage('setShowLvNameActive', SettingUI.isShowLvNameBtn);
    }

    clickKbTipBtn() {
        SettingUI.isKbTipBtn = !SettingUI.isKbTipBtn;
        Tools.setStorage('setKbTipActive', SettingUI.isKbTipBtn);
    }

    clickARBtn() {
        SettingUI.isAnswerRightBtn = !SettingUI.isAnswerRightBtn;
        Tools.setStorage('setAnswerRightActive', SettingUI.isAnswerRightBtn);
    }

    clickBtnMusic() {
        AudioManager1.isPlayBG = !AudioManager1.isPlayBG;
        AudioManager1.setMusicStorage(AudioManager1.isPlayBG);

        if (AudioManager1.isPlayBG) {
            AudioManager1.playBG("startBgm", 1);
        } else {
            AudioManager1.stopBG();
        }

        this.updateMusicBtn();
    }
    clickBtnEffect() {
        AudioManager1.isPlayAudio = !AudioManager1.isPlayAudio;
        AudioManager1.setAudioStorage(AudioManager1.isPlayAudio);
        this.updateEffectBtn();
        AudioManager1.playClick();
    }
    clickBtnVib() {
        AudioManager1.isVib = !AudioManager1.isVib;
        AudioManager1.setVibStorage(AudioManager1.isVib);
        this.updateVibBtn();
    }
    clickBtnSubtitle() {
        AudioManager1.isSubtitle = !AudioManager1.isSubtitle;
        AudioManager1.setSubtitleStorage(AudioManager1.isSubtitle);
        this.updateSubtitleBtn();
    }
    clickBtnDialogue() {
        AudioManager1.isDialogue = !AudioManager1.isDialogue;
        AudioManager1.setDialogueStorage(AudioManager1.isDialogue);
        this.updateDialogueBtn();
    }
}